/**
 * 
 */
package hx.front.manager.editor;

import hx.engine.IUserEngine;
import hx.front.manager.ManagerAction;
import hx.model.dto.User;
import hx.model.dto.UserStatus;
import hx.model.util.HXCollections;

import java.util.ArrayList;

import org.apache.log4j.Logger;

/**
 * Tareas de mantenimiento de un único usuario previamente filtrado. Alta,
 * edición y borrado.
 * 
 * @author Kineas
 * 
 */
public class UserEditor extends ManagerAction {

	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(UserEditor.class);

	private IUserEngine userEngine = null;
	
	private String id;
	private String pass;
	private String rePass;
	private String application;
	private String eMail;
	private UserStatus userStatus;
	
	private ArrayList<UserStatus> userStatusList;

	/**
	 * Entrada al action
	 */
	@Override
	public String execute() throws Exception {
		loadCollections();
		if (EDIT.equals(getAction())) { // Es un update		
			fillForm(getId());
		}
		else { // Es una creación -> Lo ponemos a pendiente.
			setUserStatus(UserStatus.PENDIENT);
			setApplication("");
		}
		return BEFORE_EDIT;
	}
	
	@Override
	public String updateCreate() throws Exception {
		
		User user = new User();
		
		//TODO: Sin control de momento, descomentar cuando la cosa eche a andar
//		if (getPass() != null && getPass().equals(getRePass()))
//			user.setPass(getPass());
//		else
//			return ERROR;
		
		user.setId(getId());
		user.setPass(getPass());
		user.setEMail(getEMail());
		user.setStatus(getUserStatus());
		user.setApplication(getApplication());

		userEngine.saveOrUpdate(user);

		return SUCCESS;
	}


	@Override
	protected void fillForm(String id) {
		
		log.info(PRE_LOAD + " User");
		
		User user = userEngine.getById(id);
		
		setId(id);
		setEMail(user.getEMail());
		setApplication(user.getApplication());
		setUserStatus(user.getStatus());
		setPass(user.getPass());
		setRePass(user.getPass());
		
		log.info(POST_LOAD + " User");			
	}
	
	@Override
	protected void loadCollections() throws Exception {
		setUserStatusList(HXCollections.USER_STATUS_LIST);
	}

	
	// Getters and setters
	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getPass() {
		return pass;
	}

	public void setRePass(String rePass) {
		this.rePass = rePass;
	}

	public String getRePass() {
		return rePass;
	}

	public void setApplication(String application) {
		this.application = application;
	}

	public String getApplication() {
		return application;
	}

	public void setEMail(String eMail) {
		this.eMail = eMail;
	}

	public String getEMail() {
		return eMail;
	}

	
	public UserStatus getUserStatus() {
		return userStatus;
	}

	public void setUserStatus(UserStatus userStatus) {
		this.userStatus = userStatus;
	}

	public IUserEngine getUserEngine() {
		return userEngine;
	}

	public void setUserEngine(IUserEngine userEngine) {
		this.userEngine = userEngine;
	}

	public ArrayList<UserStatus> getUserStatusList() {
		return userStatusList;
	}

	public void setUserStatusList(ArrayList<UserStatus> userStatusList) {
		this.userStatusList = userStatusList;
	}
	
}
